home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / WindowsComboBoxUI.java < prev    next >
Text File  |  1998-06-30  |  6KB  |  176 lines

  1. /*
  2.  * @(#)WindowsComboBoxUI.java    1.7 98/04/21
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.windows;
  22.  
  23. import com.sun.java.swing.plaf.basic.*;
  24. import com.sun.java.swing.plaf.*;
  25. import com.sun.java.swing.border.*;
  26. import com.sun.java.swing.*;
  27. import java.awt.event.*;
  28. import java.awt.*;
  29.  
  30.  
  31. /**
  32.  * Windows combo box.
  33.  * <p>
  34.  * Warning: serialized objects of this class will not be compatible with
  35.  * future swing releases.  The current serialization support is appropriate
  36.  * for short term storage or RMI between Swing1.0 applications.  It will
  37.  * not be possible to load serialized Swing1.0 objects with future releases
  38.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  39.  * baseline for the serialized form of Swing objects.
  40.  *
  41.  * @version 1.7 04/21/98
  42.  * @author Tom Santos
  43.  */
  44.  
  45. public class WindowsComboBoxUI extends BasicComboBoxUI {
  46.  
  47.     public static ComponentUI createUI(JComponent c) {
  48.         return new WindowsComboBoxUI();
  49.     }  
  50.  
  51.     public void installUI( JComponent c ) {
  52.         super.installUI( c );
  53.         comboBox.setRequestFocusEnabled( true );
  54.     }
  55.  
  56.     protected void installDefaults( JComponent c ) {
  57.         super.installDefaults( c );
  58.         comboBox.setBorder( new ComboBoxBorder() );
  59.     }
  60.  
  61.     public void paint(Graphics g, JComponent c) {
  62.         //Rectangle b = c.getBounds();
  63.         //BasicGraphicsUtils.drawEtchedRect(g,0,0,b.width,b.height);
  64.         super.paint( g, c );
  65.     }
  66.  
  67.     protected void selectNextPossibleValue() {
  68.         super.selectNextPossibleValue();
  69.     }
  70.  
  71.     protected void selectPreviousPossibleValue() {
  72.         super.selectPreviousPossibleValue();
  73.     } 
  74.  
  75.     protected void addKeyAccelerators(JComponent comp) {
  76.         super.addKeyAccelerators( comp );
  77.  
  78.         final JComboBox cBox = comboBox;
  79.  
  80.         AbstractAction downAction = new AbstractAction() {
  81.             public void actionPerformed( ActionEvent e ) {
  82.                 if ( !cBox.isEditable() ||
  83.                      (cBox.isEditable() && popupIsVisible()) ) {
  84.                     selectNextPossibleValue();
  85.                 }
  86.             }
  87.             public boolean isEnabled() {
  88.                 return cBox.isEnabled();
  89.             }
  90.         };
  91.  
  92.         comp.registerKeyboardAction( downAction,
  93.                                      KeyStroke.getKeyStroke( KeyEvent.VK_DOWN, 0 ),
  94.                                      JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
  95.  
  96.         AbstractAction upAction = new AbstractAction() {
  97.             public void actionPerformed( ActionEvent e ) {
  98.                 if ( !cBox.isEditable() ||
  99.                      (cBox.isEditable() && popupIsVisible()) ) {
  100.                     selectPreviousPossibleValue();
  101.                 }
  102.             }
  103.             public boolean  isEnabled() {
  104.                 return cBox.isEnabled();
  105.             }        
  106.         };
  107.  
  108.         comp.registerKeyboardAction( upAction,
  109.                                      KeyStroke.getKeyStroke( KeyEvent.VK_UP, 0 ),
  110.                                      JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
  111.     }
  112.  
  113.     protected void removeKeyAccelerators(JComponent comp) {
  114.         super.removeKeyAccelerators( comp );
  115.         comp.unregisterKeyboardAction(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,0));
  116.         comp.unregisterKeyboardAction(KeyStroke.getKeyStroke(KeyEvent.VK_UP,0));
  117.     }
  118.  
  119.     protected class ComboBoxBorder implements Border, UIResource {
  120.         public void paintBorder(Component c, Graphics g, int x, int y, int width, int height){
  121.             Rectangle b = c.getBounds();
  122.             BasicGraphicsUtils.drawEtchedRect(g,0,0,b.width,b.height);
  123.         }
  124.  
  125.         public Insets getBorderInsets(Component c) {
  126.             return new Insets( 2, 2, 2, 2 );
  127.         }
  128.  
  129.         public boolean isBorderOpaque() {
  130.             return true;
  131.         }
  132.     }
  133.  
  134.     protected ComboPopup createPopup() {
  135.         return new WindowsComboPopup( comboBox );
  136.     }
  137.  
  138.     protected class WindowsComboPopup extends BasicComboPopup {
  139.         // This is here because the compiler isn't currently letting this
  140.         // inner class have access to its parent's inherited data members.
  141.         JComboBox comboBox;
  142.  
  143.         public WindowsComboPopup( JComboBox cBox ) {
  144.             super( cBox );
  145.             comboBox = cBox;
  146.         }
  147.  
  148.         protected KeyListener createKeyListener() {
  149.             return new WindowsKeyListener();
  150.         }
  151.  
  152.         protected class WindowsKeyListener extends KeyAdapter {
  153.             public void keyReleased( KeyEvent e ) {
  154.                 if ( e.isShiftDown() && e.getKeyCode() != KeyEvent.VK_SHIFT ) {
  155.                     if ( e.getKeyCode() == KeyEvent.VK_UP ||
  156.                          e.getKeyCode() == KeyEvent.VK_DOWN ) {
  157.                         if ( isVisible() ) {
  158.                             hide();
  159.                         }
  160.                         else {
  161.                             show();
  162.                         }
  163.                     }
  164.                 }
  165.                 else if ( comboBox.isEditable() &&
  166.                           !isVisible() &&
  167.                           (e.getKeyCode() == KeyEvent.VK_UP ||
  168.                            e.getKeyCode() == KeyEvent.VK_DOWN) ) {
  169.                     show();
  170.                 }
  171.             }
  172.         }
  173.     }
  174. }
  175.  
  176.